#!/user/bin/env python3
# -*- coding: utf-8 -*-

from flask import Response
from flask_restful import Resource, reqparse
from src.formats.MapServiceManager import mapServiceManager

class ESRIBunlde(Resource):
    def __init__(self):
        self.parser = reqparse.RequestParser()
        self.parser.add_argument('callback', type=str, location='args')
        print('init BundleMapServer')

    # get请求
    def get(self, servicename):
        data = self.parser.parse_args()
        callback = data.get('callback')
        resp = ''
        try:
            if mapServiceManager.serviceList[servicename]:
                resp = mapServiceManager.MapServerJSON
                if 'MapServerJSONPath' in mapServiceManager.serviceList[servicename]['config']:
                    resp = mapServiceManager.serviceList[servicename]['config']['MapServerJSON']
                if (callback):
                    resp = callback + '(' + resp + ');'
                    resp = Response(resp, status=200, mimetype='text/javascript')
                else:
                    resp = Response(resp, status=200, mimetype='application/json')
        except Exception as e:
            print(e)
            resp = Response(servicename + ' Not Found',
                            status=404,
                            mimetype='text/javascript')
        finally:
            resp.headers['Access-Control-Allow-Origin'] = '*'
            resp.headers['Access-Control-Allow-Headers'] = 'Content-Type'
            resp.headers['Access-Control-Allow-Credentials'] = 'GET,POST,OPTIONS'

            return resp
